package com.vdg.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;

import android.content.Context;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.SectionIndexer;

import com.handmark.pulltorefresh.library.TextViewPlus;
import com.vdg.data.AppInformation;
import com.vdg.lockapp.R;
import com.vdg.util.ImageLoader;

public class ListAppAdapter extends BaseAdapter implements SectionIndexer {
	private ArrayList<AppInformation> mListApplication;
	private Context mContext;
	ImageLoader mImageLoader;
	private HashMap<String, Integer> alphaIndexer;
	private String[] sections;

	public ListAppAdapter(Context context, int textViewResourceId,
			ArrayList<AppInformation> objects) {
		super();
		// TODO Auto-generated constructor stub
		mListApplication = objects;
		mContext = context;
		mImageLoader = ImageLoader.getInstance(mContext);
		if (mListApplication != null) {
			alphaIndexer = new HashMap<String, Integer>();
			for (int i = 0; i < mListApplication.size(); i++) {
				String name = mListApplication.get(i)
						.loadLabel(mContext.getPackageManager()).toString();
				if (mListApplication.get(i) != null && name != null&&name.length()>0) {
					String s = name.substring(0, 1).toUpperCase();
					if (!alphaIndexer.containsKey(s))
						alphaIndexer.put(s, i);
				}
			}
			Set<String> sectionLetters = alphaIndexer.keySet();
			ArrayList<String> sectionList = new ArrayList<String>(
					sectionLetters);
			Collections.sort(sectionList);
			sections = new String[sectionList.size()];
			for (int i = 0; i < sectionList.size(); i++)
				sections[i] = sectionList.get(i);
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		AppInformation appInfor = mListApplication.get(position);
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.application_row, parent, false);
			holder.mTxvAppName = (TextViewPlus) convertView
					.findViewById(R.id.txv_appname);
			holder.mImvIcon = (ImageView) convertView
					.findViewById(R.id.imv_icon);
			holder.mChbSelected = (CheckBox) convertView
					.findViewById(R.id.chb_selected);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (appInfor != null) {
			String name = appInfor.getName();
			holder.mTxvAppName.setText(name);
			mImageLoader.loadImage(holder.mImvIcon, appInfor, name);
			if (appInfor.isIsSelected()) {
				holder.mChbSelected.setChecked(true);
			} else {
				holder.mChbSelected.setChecked(false);
			}
		}
		return convertView;
	}

	public static class ViewHolder {
		public CheckBox mChbSelected;
		private TextViewPlus mTxvAppName;
		private ImageView mImvIcon;
	}

	@Override
	public int getPositionForSection(int arg0) {
		return alphaIndexer.get(sections[arg0]);
	}

	@Override
	public int getSectionForPosition(int position) {
		return 1;
	}

	@Override
	public Object[] getSections() {
		return sections;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (mListApplication == null) {
			return 0;
		}
		return mListApplication.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mListApplication.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return mListApplication.indexOf(getItem(position));
	}
}
